package com.walkthecow.justdraw;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class WidthPickerDialog extends Dialog implements OnSeekBarChangeListener, OnClickListener  {

	private int mWidth;
	private int mInitialWidth;
	private OnWidthChangedListener mListener;
	private TextView textView;
	private SeekBar seekbar;
	private Button buttonOK;
	private Button buttonCancel;

	public interface OnWidthChangedListener {
		void widthChanged(int width);
	}

	public WidthPickerDialog(Context context, OnWidthChangedListener listener, int initialWidth) {
		super(context);

		mListener = listener;
		mInitialWidth = initialWidth;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.width_picker_dialog_layout);
		setTitle("Change Width");

		seekbar = (SeekBar) findViewById(R.id.width_seekbar);
		seekbar.setProgress(mInitialWidth);

		textView = (TextView) findViewById(R.id.width_textview);
		textView.setText("Width = " + mInitialWidth);

		seekbar.setOnSeekBarChangeListener(this);

		buttonOK = (Button) findViewById(R.id.button_ok);
		buttonOK.setOnClickListener(this);

		buttonCancel = (Button) findViewById(R.id.button_cancel);
		buttonCancel.setOnClickListener(this);
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
		// change progress text label with current seekbar value
		textView.setText("Width = " + progress);
		mWidth = progress;
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		// set the shade of the previous value.
		seekBar.setSecondaryProgress(seekBar.getProgress()); 
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.button_cancel:
			dismiss();
			break;
		case R.id.button_ok:
			mListener.widthChanged(mWidth);
			dismiss();
			break;
		}
	}
}